using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace jSLCore.Client.Presentation.Controls
{
    public class ErrorBorder : ContentControl
    {
        public ErrorBorder()
        {
            DefaultStyleKey = typeof (ErrorBorder);
            
            ToolTip = new ToolTip(){Placement = PlacementMode.Right};

            Loaded += (o, eventArgs) =>
                          {
                              FrameworkElement element = null;
                                  var nullableBox = Content as jNullableBox;
                                  if (nullableBox != null) element = ((jNullableBox)nullableBox).Content as FrameworkElement;
                                  if (element == null) element = Content as FrameworkElement;
                                  if (element != null)
                                  {
                                      element.LayoutUpdated += (sender, args) =>
                                                                   {
                                                                       if (firstApply)
                                                                       {
                                                                           Margin = element.Margin;
                                                                           element.Margin = new Thickness(0);
                                                                           firstApply = false;
                                                                       }
                                                                   };
                                  }

                              /*LayoutUpdated += (sender, args) =>
                              {
                                  FrameworkElement element = null;
                                  var nullableBox = Content as jNullableBox;
                                  if (nullableBox != null) element = ((jNullableBox)nullableBox).Content as FrameworkElement;
                                  if (element == null) element = Content as FrameworkElement;
                                  if (element != null)
                                  {
                                      Margin = element.Margin;
                                      element.Margin = new Thickness(0);
                                  }
                              };*/
                          };
        }

        private bool firstApply = true;
#if SILVERLIGHT
        public static readonly DependencyProperty ToolTipProperty = DependencyProperty.Register("ToolTip",
                                                                                                typeof (ToolTip),
                                                                                                typeof (ErrorBorder),
                                                                                                new PropertyMetadata(new ToolTip {Placement = PlacementMode.Right}));

       

        public ToolTip ToolTip
        {
            get { return (ToolTip)GetValue(ToolTipProperty); }
            set {SetValue(ToolTipProperty,value);}
        }
#else
        public static readonly DependencyProperty ToolTipProperty = DependencyProperty.Register("ToolTip",
                                                                                               typeof(ToolTip),
                                                                                               typeof(ErrorBorder)/*,
                                                                                               new PropertyMetadata(new ToolTip { Placement = PlacementMode.Right })*/);
        public ToolTip ToolTip
        {
            get { return (ToolTip)GetValue(ToolTipProperty); }
            set { SetValue(ToolTipProperty, value); }
        }
#endif

    }
}